#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  02-attributes.sh
# Version	:  1.0
# Date		:  2025/02/06
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinos.com.cn
# Function	:  GN-WJXT_004文件和目录的属性及基本操作
# Out		:
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="GN-WJXT_004文件和目录的属性及基本操作"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB() {
    # 要求文件系统是ext4类型
    df -Th | grep -w / | grep ext4
    CommRetParse_LTFLIB "df -Th|grep -w /|grep ext4"
    testfile="/test.txt"
    touch "${testfile}"
    CommRetParse_LTFLIB "touch ${testfile}"
    stat ${testfile}
    CommRetParse_LTFLIB "stat ${testfile}"
    return $TPASS
}

## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB() {
    Debug_LLE "rm -rf ${testfile}"
    rm -rf ${testfile}
    return $TPASS
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB() {
    testcase_1
    testcase_2
    testcase_3
    return $TPASS
}

## TODO ： GN-WJXT_004文件和目录的属性及基本操作
testcase_1() {
    file *
    CommRetParse_LTFLIB "file *"

    local txt_inode=$(stat ${testfile} | grep Inode: | sed -n 's/.*Inode: \([0-9]*\).*/\1/p')
    debugfs -V
    CommRetParse_LTFLIB "debugfs -V"
    # 间隔5秒后修改文件
    sleep 5s
    echo "abc" >"${testfile}"
    CommRetParse_LTFLIB "echo \"abc\">${testfile}"
    local dev_root=$(df -Th | grep -w / | awk '{print $1}')
    debugfs -R "stat <${txt_inode}>" "${dev_root}"
    CommRetParse_LTFLIB "debugfs -R \"stat <${txt_inode}>\" ${dev_root}"
    #这里查看文件修改时间和创建时间不一样
    # 获取文件状态信息
    local stat_output=$(stat --format="%W %Y" "$testfile")
    local creation_time=$(echo "$stat_output" | cut -d' ' -f1)
    local modification_time=$(echo "$stat_output" | cut -d' ' -f2)
    # 比较时间
    if [ "${creation_time}" -ne "${modification_time}" ]; then
        OutputRet_LTFLIB ${TPASS}
        TestRetParse_LTFLIB "${testfile}的修改时间与创建时间不一样"
    else
        OutputRet_LTFLIB ${TFAIL}
        TestRetParse_LTFLIB "${testfile}的修改时间与创建时间仍一样"
    fi
    #显示信息
    stat "${testfile}"
    CommRetParse_LTFLIB "stat ${testfile}"

    local file_1=$(cat "${testfile}")
    local file_2=$(cat "${testfile}")
    if [ "${file_1}" == "abc" ] && [ "${file_2}" == "abc" ]; then
        OutputRet_LTFLIB ${TPASS}
        TestRetParse_LTFLIB "重读两次${testfile}，内容一致"
    else
        OutputRet_LTFLIB ${TFAIL}
        TestRetParse_LTFLIB "重读两次${testfile}，内容不一致"
    fi
    echo "123" >"${testfile}"
    CommRetParse_LTFLIB "echo \"123\">${testfile}"
    local file_3=$(cat "${testfile}")
    if [ "${file_3}" == "123" ]; then
        OutputRet_LTFLIB ${TPASS}
        TestRetParse_LTFLIB "修改${testfile}，内容一致"
    else
        OutputRet_LTFLIB ${TFAIL}
        TestRetParse_LTFLIB "修改${testfile}，内容不一致"
    fi
    echo "678" >"${testfile}"
    CommRetParse_LTFLIB "echo \"678\">${testfile}"
    local file_4=$(cat "${testfile}")
    if [ "${file_4}" == "678" ]; then
        OutputRet_LTFLIB ${TPASS}
        TestRetParse_LTFLIB "修改${testfile}，内容一致"
    else
        OutputRet_LTFLIB ${TFAIL}
        TestRetParse_LTFLIB "修改${testfile}，内容不一致"
    fi
}

## TODO ： GN-WJXT_004文件和目录的属性及基本操作
testcase_2() {
    #创建目录与文件
    mkdir ${TmpTestDir_LTFLIB}/ext4_04
    CommRetParse_LTFLIB "mkdir ${TmpTestDir_LTFLIB}/ext4_04"
    mkdir ${TmpTestDir_LTFLIB}/ext4_04/testdir
    CommRetParse_LTFLIB "mkdir ${TmpTestDir_LTFLIB}/ext4_04/testdir"
    touch ${TmpTestDir_LTFLIB}/ext4_04/testdir/testfile
    CommRetParse_LTFLIB "touch ${TmpTestDir_LTFLIB}/ext4_04/testdir/testfile"
    #复制
    cp -r ${TmpTestDir_LTFLIB}/ext4_04/testdir ${TmpTestDir_LTFLIB}
    CommRetParse_LTFLIB "cp -r ${TmpTestDir_LTFLIB}/ext4_04/testdir ${TmpTestDir_LTFLIB}"
    ls ${TmpTestDir_LTFLIB}/testdir | grep testfile
    CommRetParse_LTFLIB "ls ${TmpTestDir_LTFLIB}/testdir | grep testfile"
    #移动
    mkdir ${TmpTestDir_LTFLIB}/ext4_04_mv
    CommRetParse_LTFLIB "mkdir ${TmpTestDir_LTFLIB}/ext4_04_mv"
    mv ${TmpTestDir_LTFLIB}/ext4_04/testdir ${TmpTestDir_LTFLIB}/ext4_04_mv
    CommRetParse_LTFLIB "mv ${TmpTestDir_LTFLIB}/ext4_04/testdir ${TmpTestDir_LTFLIB}/ext4_04_mv"
    ls ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir
    CommRetParse_LTFLIB "ls ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir"
    #重命名
    mv ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename
    CommRetParse_LTFLIB "mv ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename"
    ls ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename
    CommRetParse_LTFLIB "ls ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename"
    #设置权限
    ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename
    CommRetParse_LTFLIB "ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename"
    chmod 000 ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename
    CommRetParse_LTFLIB "chmod 000 ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename"
    ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename | grep "\---------"
    CommRetParse_LTFLIB "ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename | grep \---------"
    chmod 777 ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename
    CommRetParse_LTFLIB "chmod 777 ${TmpTestDir_LTFLIB}/ext4_04_mv/testdir_rename"
    ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename | grep rwxrwxrwx
    CommRetParse_LTFLIB "ls -l ${TmpTestDir_LTFLIB}/ext4_04_mv | grep testdir_rename | grep rwxrwxrwx"
}

## TODO ： GN-WJXT_004文件和目录的属性及基本操作
testcase_3() {
    #目录_读
    cd /etc | head -n 10
    CommRetParse_LTFLIB "cd /etc | head -n 10"
    ls -la
    CommRetParse_LTFLIB "ls -la"
    cat /etc/sudoers | head -n 10
    CommRetParse_LTFLIB "cat /etc/sudoers | head -n 10"
    #目录_写
    mkdir -p ${TmpTestDir_LTFLIB}/ext4_04_test
    CommRetParse_LTFLIB "mkdir -p ${TmpTestDir_LTFLIB}/ext4_04_test"
    cd ${TmpTestDir_LTFLIB}/ext4_04_test
    CommRetParse_LTFLIB "cd ${TmpTestDir_LTFLIB}/ext4_04_test"
    touch ext4_test.txt
    CommRetParse_LTFLIB "touch ext4_test.txt"
    ls -l ${TmpTestDir_LTFLIB}/ext4_04_test | grep ext4_test.txt
    CommRetParse_LTFLIB "ls -l ${TmpTestDir_LTFLIB}/ext4_04_test | grep ext4_test.txt"
    #定位文件
    updatedb
    CommRetParse_LTFLIB "updatedb"
    locate anaconda-ks.cfg
    CommRetParse_LTFLIB "locate anaconda-ks.cfg"
    #删除目录
    rm -rf ${TmpTestDir_LTFLIB}/ext4_04_test
    CommRetParse_LTFLIB "rm -rf ${TmpTestDir_LTFLIB}/ext4_04_test"
    ! ls -la ${TmpTestDir_LTFLIB} | grep ext4_04_test
    CommRetParse_LTFLIB "! ls -la ${TmpTestDir_LTFLIB} | grep ext4_04_test"
}
#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
